package com.aidong.login.biz.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aidong.login.biz.MemberReigsterBiz;
import com.aidong.login.dao.MemberReigsterDao;
import com.aidong.util.RandomUtil;

@Service("memberBiz")
public class MemberReigsterImpl implements MemberReigsterBiz {
    private MemberReigsterDao trnMemberDao;

    @Autowired
    public void setTrnMemberDao(MemberReigsterDao trnMemberDao) {
        this.trnMemberDao = trnMemberDao;
    }

    /**
     * 验证此手机号码是否已注册
     */
    @Override
    public int getRegistered(String phoneNo) {
        return trnMemberDao.getRegistered(phoneNo);
    }

    /**
     * 添加新用户
     */
    @Override
    public void addMember(Map<String, Object> map) {
        Date date = new Date(); // 得到系统时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String time = format.format(date);
        map.put("createTime", time);
        trnMemberDao.addMember(map);
    }

    /**
     * 给新注册的用户发送优惠券
     *
     * @param map
     */
    @Transactional
    public void addTicketsByNewbie(Map<String, Object> map) {
        try {
            SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
            Date beginTime = sm.parse("2016-11-29");
            Date endTime = sm.parse("2016-12-06");
            String time = sm.format(new Date());
            Date nowDate = sm.parse(time);
            if (nowDate.getTime() >= beginTime.getTime() && nowDate.getTime() <= endTime.getTime()) {
                //添加代金券
                Map<String, Object> newbieMap = new HashMap<String, Object>();
                String ticketNo = RandomUtil.generateShortUuid(8);
                newbieMap.put("couponsNum", ticketNo);
                newbieMap.put("couponsPrice", 10);
                newbieMap.put("validDateBegin", time);
                newbieMap.put("validDateEnd", "2016-12-31");
                newbieMap.put("status", 1);
                newbieMap.put("delFlg", 0);
                trnMemberDao.addCouponsByNewbie(newbieMap);
                //添加系统消息
                newbieMap.clear();
                newbieMap.put("title", "感谢您注册成为爱动大家族的成员~！");
                StringBuffer sb = new StringBuffer("代金券码：" + ticketNo);
                sb.append("\r\n代金券使用规则：");
                sb.append("\r\n1.新注册用户即可获得一张10元代金券（消费金额无门槛）");
                sb.append("\r\n2.本代金券只限购买小团体课，营养品，装备时使用");
                sb.append("\r\n3.一个帐号只限一张代金券");
                sb.append("\r\n4.代金券有效期至2016年12月31日23:59:59，过期作废");
                sb.append("\r\n5.代金券不可找零，不可兑现");
                sb.append("\r\n*爱动APP保留对代金券活动的最终解释权");
                newbieMap.put("content", sb.toString());
                newbieMap.put("idongId", map.get("idongId"));
                newbieMap.put("delFlg", 0);
                trnMemberDao.addSysInfoByNewbie(newbieMap);
                String sysInfoId = newbieMap.get("SysInfoID").toString();
                newbieMap.clear();
                newbieMap.put("sysInfoId", sysInfoId);
                newbieMap.put("idongId", map.get("idongId"));
                newbieMap.put("status", 0);
                newbieMap.put("delFlg", 0);
                trnMemberDao.addSysInfoForUser(newbieMap);

                trnMemberDao.updateUserMsg(newbieMap);
            }
        } catch (Exception exp) {
        }
    }
}
